<?php

namespace App\Models\OpenShop\InvestmentPromotion;

use App\Enums\OpenShop\InvestmentPromotion\UploadFileResourceEnum;
use App\Models\BaseModel;
use App\Models\Traits\KfUid;
use App\Service\OpenShop\Feishu\SiteMissionService;
use App\Service\OpenShop\OrderService;
use App\Service\OpenShop\Wap\SignOrderService;
use App\Support\ChangeSupport;
use Illuminate\Database\Eloquent\Relations\HasOne;
use function Symfony\Component\String\s;

class OpenShopLocationModel extends BaseModel
{
    use KfUid;

    protected $table = 'open_shop_location';

    const SUBMIT_REPORT_STATUS_ON  = 1; //已确认
    const SUBMIT_REPORT_STATUS_OFF = 0; //待确认

    const CONFIRM_STATUS_ON  = 1; //商家已确认
    const CONFIRM_STATUS_OFF = 0; //商家未确认

    const CASH_TYPE_1 = 1;
    const CASH_TYPE_2 = 2;
    const CASH_TYPE_3 = 3;
    const CASH_TYPE_4 = 4;

    const CODE_CN = [
        self::CASH_TYPE_1 => '第三方收银',
        self::CASH_TYPE_2 => '夸父收银',
        self::CASH_TYPE_3 => '美团收银',
//        self::CASH_TYPE_4 => '哗啦啦收银',
    ];

    const MONTH_RENT_0 = 0; // 月租金
    const MONTH_RENT_1 = 1; // 纯扣百分比
    const MONTH_RENT_2 = 2; // 月租+百分比
    const MONTH_RENT_3 = 3; // 月租/纯扣取最高

    // 评估类型
    const LOCATION_TYPE_0   = 0; // 未选择
    const LOCATION_TYPE_1   = 1; // 现场评估
    const LOCATION_TYPE_2   = 2; // 远程评估
    const LOCATION_TYPE_MAP = [
        self::LOCATION_TYPE_0 => "未选择",
        self::LOCATION_TYPE_1 => "现场评估",
        self::LOCATION_TYPE_2 => "远程评估"
    ];

    // 用电增容
    const IS_ADD_ELECTRIC_NUM_0    = 0; // 否
    const IS_ADD_ELECTRIC_NUM_1    = 1; // 是
    const IS_ADD_ELECTRIC_TYPE_MAP = [
        self::IS_ADD_ELECTRIC_NUM_0 => "否",
        self::IS_ADD_ELECTRIC_NUM_1 => "是",
    ];

    // 是否排烟改造
    const IS_REFORM_SMOKE_0        = 0; // 否
    const IS_REFORM_SMOKE_1        = 1; // 是
    const IS_REFORM_SMOKE_TYPE_MAP = [
        self::IS_REFORM_SMOKE_0 => "否",
        self::IS_REFORM_SMOKE_1 => "是",
    ];

    // 是否支持上下水管径改造
    const IS_REFORM_WATER_0        = 0; // 否
    const IS_REFORM_WATER_1        = 1; // 是
    const IS_REFORM_WATER_TYPE_MAP = [
        self::IS_REFORM_WATER_0 => "否",
        self::IS_REFORM_WATER_1 => "是",
    ];

    const MONTH_RENT_TYPE = [
        self::MONTH_RENT_0 => "月租金",
        self::MONTH_RENT_1 => "纯扣百分比",
        self::MONTH_RENT_2 => "月租+百分比",
        self::MONTH_RENT_3 => "月租/纯扣取最高"
    ];


    // 是否自带选址
    const IS_SELF_LOCATION_0        = 0; // 否
    const IS_SELF_LOCATION_1        = 1; // 是
    const IS_SELF_LOCATION_TYPE_MAP = [
        self::IS_SELF_LOCATION_0 => "否",
        self::IS_SELF_LOCATION_1 => "是",
    ];

    protected $appends = [
        'cashTypeText'
    ];

    public static function getOrderLogTranslate(): array
    {
        return [
            "submitReportTime"      => [
                "type" => ChangeSupport::FILED_TYPE_DATE_YMD_HIS,
                "desc" => "提交时间"
            ],
            "shopLocationReportUrl" => [
                "type" => ChangeSupport::FILED_TYPE_PRIVATE_FILE,
                "desc" => "选址报告"
            ],
            "shopDetailAddress"     => "门店详细地址",
            "rentPer"               => "月租金百分比",
            "monthlyRent"           => [
                "type" => ChangeSupport::FILED_TYPE_SELECT_ENUM,
                "enum" => self::MONTH_RENT_TYPE,
                "desc" => "月租金类型"
            ],
            "shopArea"              => "门店面积",
            "shopRent"              => [
                "type" => ChangeSupport::FILED_TYPE_PRICE,
                "desc" => "门店租金"
            ],
            "shopType"              => "店型",
            "averageDailyBreak"     => [
                "type" => ChangeSupport::FILED_TYPE_PRICE,
                "desc" => "日均保本点"
            ],
            "averageDaily"          => [
                "type" => ChangeSupport::FILED_TYPE_PRICE,
                "desc" => "日均收益"
            ],
            "payMonth"              => "支付月数",
            "depositMonth"          => "押金月数",
            "locationType"          => [
                "desc" => "评估类型",
                "enum" => self::LOCATION_TYPE_MAP,
                "type" => ChangeSupport::FILED_TYPE_ENUM
            ],
            "shopScopeProducts"     => "产品限制销售范围",
            "electricQuantity"      => "用电量",
            "isAddElectricNum"      => [
                "desc" => "是否支持用电量增容",
                "enum" => self::IS_ADD_ELECTRIC_TYPE_MAP,
                "type" => ChangeSupport::FILED_TYPE_ENUM
            ],
            "smokeNumber"           => "排烟量",
            "isReformSmoke"         => [
                "desc" => "是否支持排烟改造",
                "enum" => self::IS_REFORM_SMOKE_TYPE_MAP,
                "type" => ChangeSupport::FILED_TYPE_ENUM
            ],
            "upperWaterDiameter"    => "上水管径",
            "lowerWaterDiameter"    => "下水管径",
            "isReformWater"         => [
                "desc" => "是否支持上下水管径改造",
                "enum" => self::IS_REFORM_WATER_TYPE_MAP,
                "type" => ChangeSupport::FILED_TYPE_ENUM
            ],
            "isLocationSelf"        => [
                "desc" => "是否自带选址",
                "enum" => self::IS_SELF_LOCATION_TYPE_MAP,
                "type" => ChangeSupport::FILED_TYPE_ENUM
            ],
            "cashType"              => [
                "desc" => "收银方式",
                "enum" => self::CODE_CN,
                "type" => ChangeSupport::FILED_TYPE_ENUM
            ],
            "openTime"              => [
                "type" => ChangeSupport::FILED_TYPE_DATE_YMD_HIS,
                "desc" => "开店时间"
            ],
            "shopAddressPrefix"     => "门店地址 【省市区】"
        ];
    }

    public function getCashTypeTextAttribute()
    {
        return self::CODE_CN[$this->cashType] ?? '暂未选择';
    }

    protected $fillable = [
        'mainProcessId',
        'step',
        'orderNo',
        'investmentManagerName',
        'investmentManagerId',
        'assignTime',
        'shopLocationReportUrl',
        'shopLocationReportLink',
        'submitReportTime',
        'confirmStatus',
        'confirmTime',
        'createdAt',
        'createdByUid',
        'updatedAt',
        'updatedByUid',
        'shopAddressPrefix',
        'provinceCode',
        'cityCode',
        'districtCode',
        'shopDetailAddress',
        'rentPer',
        'monthlyRent',
        'shopRent',
        'shopArea',
        'shopType',
        'averageDailyBreak',
        'averageDaily',
        'isLocationSelf',
        'payMonth',
        'depositMonth',
        'locationType',
        'shopScopeProducts',
        'electricQuantity',
        'isAddElectricNum',
        'smokeNumber',
        'isReformSmoke',
        'upperWaterDiameter',
        'lowerWaterDiameter',
        'isReformWater',
        'cashType',
        'openTime',
        'deposit'
    ];

    public function getInvestmentManager(string $orderNo)
    {
        return self::getInvestmentInfo($orderNo, ['investmentManagerName', 'investmentManagerId']);
    }

    public function getInvestmentLocation(string $orderNo)
    {
        return self::getInvestmentInfo($orderNo, [
            'averageDailyBreak',
            'averageDaily',
            'rentPer',
            'monthlyRent',
            'isLocationSelf',
            'payMonth',
            'depositMonth',
            'locationType',
            'shopLocationReportUrl',
            'shopAddressPrefix',
            'provinceCode',
            'cityCode',
            'districtCode',
            'shopDetailAddress',
            'shopRent',
            'shopArea',
            'shopType',
            'shopScopeProducts',
            'openTime',
            'cashType',
            'shopLocationReportLink',
            'electricQuantity',
            'isAddElectricNum',
            'smokeNumber',
            'isReformSmoke',
            'upperWaterDiameter',
            'lowerWaterDiameter',
            'isReformWater',
            'orderNo'
        ]);
    }

    public function fileRel()
    {
        return self::hasMany(OpenShopUploadFileResourceModel::class, 'orderNo', 'orderNo');
    }

    public function getInvestmentInfo(string $orderNo, $field = [])
    {
        return self::query()
                   ->with([
                       'fileRel' => function ($query) {
                           $query->where('isDeleted', 0)
                                 ->whereIn('position', UploadFileResourceEnum::SELECT_LOCATION_LIST);
                       }
                   ])
                   ->where('orderNo', $orderNo)->select($field)
                   ->first();
    }

    /**
     * 找指派人
     *
     * @param string $orderNo
     * @return int
     */
    public function getAssignUid(string $orderNo)
    {
        return (int)optional(self::getInvestmentInfo($orderNo, ['investmentManagerId']))->investmentManagerId;
    }

    /**
     * @return HasOne
     */
    public function organizationRegionMappingRel(): HasOne
    {
        return $this->hasOne(OpenShopOrganizationRegionMappingModel::class, "provinceCode", "provinceCode");
    }
}
